var app = getApp();
var iFDU = require('../../utils/iFDU.js');
Page({

  data: {
    config: app.config,
    globalData: app.globalData,
    orderId: 0,
    showRedbag: false,
    showQRCode: false,
    virRedBagId: 0,
    virRedBagInfo: {},
  },

  onLoad: function (options) {
    var that = this;
    var virRedBagId = options.virRedBagId;
    if (virRedBagId && virRedBagId > 0) {
      this.setData({
        orderId: options.orderId,
        virRedBagId: virRedBagId,
        showRedbag: true
      });

      app.getUserInfo(function (globalData) {
        that.setData({
          globalData: globalData
        });

        iFDU.api.doGet(
          'redbag/getVirtualRedbagById',
          {
            userId: globalData.userId,
            virtualRedbagId: that.data.virRedBagId,
          },
          function (data) {
            that.setData({
              virRedBagInfo: data.virtualRedbagView
            })
          }
        )
      });
    } else {
      this.setData({
        orderId: options.orderId
      });

      var queryCount = 0;
      var timer = setInterval(function() {
        app.getUserInfo(function (globalData) {
          that.setData({
            globalData: globalData
          });

          iFDU.api.doGet(
            'redbag/getVirtualRedbagByOrderId',
            {
              userId: globalData.userId,
              orderId: that.data.orderId,
            },
            function (data) {
              var virtualRedbagView = data.virtualRedbagView;
              if (virtualRedbagView) {
                clearInterval(timer);
                that.setData({
                  virRedBagId: virtualRedbagView.id,
                  virRedBagInfo: virtualRedbagView,
                  showRedbag: true
                });
              } else if (queryCount > 5) {
                clearInterval(timer);
              } else {
                queryCount++;
              }
              
              // if (virtualRedbagView) {
              //   iFDU.api.doGet(
              //     'redbag/getVirtualRedbagById',
              //     {
              //       userId: globalData.userId,
              //       virtualRedbagId: virtualRedbagView.id,
              //     },
              //     function (data) {
              //       that.setData({
              //         virRedBagInfo: data.virtualRedbagView
              //       })
              //     }
              //   )
              // }
            }
          )
        });
      }, 1000);
    }
  },

  onShow: function () {

  },

  showDetail: function (e) {
    var that = this;
    wx.redirectTo({
      url: '../order-detail/order-detail?orderId=' + that.data.orderId,
    })
  },

  goHome: function () {
    wx.switchTab({
      url: '../home/home',
    })
  },

  goShare: function () {
    // wx.navigateTo({
    //   url: '../redbag-detail/redbag-detail?virtualRedbagId=' + this.data.virRedBagId,
    // })
    wx.navigateTo({
      url: '../share-redbag/share-redbag',
    })
  },

  showQRCode: function () {
    this.setData({
      showQRCode: true
    })
  },

  hideQRCode: function () {
    this.setData({
      showQRCode: false
    })
  },

  showRedbag: function () {
    this.setData({
      showRedbag: true
    })
  },

  hideRedbag: function () {
    this.setData({
      showRedbag: false
    })
  },

  onShareAppMessage: function (res) {
    var that = this;
    return {
      title: '爱上衬衫，从「逸见」开始…',
      imageUrl: 'http://s.i-fdu.com/runto/wxa/s/yjshare.jpeg',
      path: 'pages/home/home',
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  }
})